package com.beici.seoserver.controller;

import com.beici.seoserver.model.node;
import com.beici.seoserver.model.resultes;
import com.beici.seoserver.model.user;
import com.beici.seoserver.service.classBoxInterface.classBoxAlbumService;
import com.beici.seoserver.service.classBoxInterface.classBoxNodeService;
import com.beici.seoserver.service.nodeServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 视图响应控制器
 * 用来响应百度 谷歌  等 搜索引擎的爬虫处理
 * @auther WaterGhost
 * @date 2022/2/24 13:59
 */
@Controller
public class viewConotroller {

    @Autowired
    private nodeServiceImpl nodeService;
    @Autowired
    private classBoxNodeService boxNodeService;
    @Autowired
    private classBoxAlbumService boxAlbumService;

    @GetMapping("/detail/{nodeId}")
    public String getDetailNode(@PathVariable("nodeId") Long nodeId, Model model, HttpServletRequest request){
        resultes resultes = nodeService.queryDetailsInfo(nodeId);
        if (resultes.getCode()==200){
            // 响应视图
            model.addAttribute("nodeInfo",resultes.getData());
            return "details";
        }
        // 返回错误码
        return null;
    }

    // 个人中心
    @GetMapping("/personcenter/{userId}")
    public String getFriendsDetils(@PathVariable("userId") Integer userId,Model model){
        // 获取用户信息
        resultes resultes = nodeService.quertyUserNodes(userId);
        user user = (com.beici.seoserver.model.user) nodeService.selectUserDetailInfo(Long.valueOf(userId)).getData();
        List<node> data = (ArrayList) resultes.getData();
        if (!data.isEmpty()){
            for (node datum : data) {
                datum.setUserHeader(user.getUserHeader());
                datum.setUserName(user.getUserName());
                // 将图片转为ArryList
                String[] split = datum.getNodeImages().split(",");
                datum.setArrImgs(split);
            }
        }else {
            data=null;
        }
        // 获取用户文章信息
        model.addAttribute("userInfo",user);
        model.addAttribute("nodes",data);
        return "personcenter";
    }

    // 个人中心
    @GetMapping("/")
    public String getHome(Model model){
        List<node> resultes = nodeService.quertyNodeAll();
        model.addAttribute("nodes",resultes);
        return "home";
    }

    @GetMapping("/album/{albumid}")
    public String getFriendsDetils(@PathVariable("albumid") Long albumId,Model model){
        resultes resultes = nodeService.quertyDetilsTag(albumId);
        model.addAttribute("album",resultes.getData());
        return "album";
    }

    @GetMapping("/community")
    public String community(Model model){
        resultes nodes = boxNodeService.getNodes();
        resultes albumAll =boxAlbumService.getAlbumAll();
        model.addAttribute("nodes",nodes.getData());
        model.addAttribute("albums",albumAll.getData());
        return "community";
    }
    @GetMapping("/forum/{cbId}")
    public String community(@PathVariable("cbId") Long cbId,Model model){

        resultes nodeDetails = boxNodeService.getNodeDetails(cbId);
        model.addAttribute("nodes",nodeDetails.getData());
        return "forum";
    }

}
